package com.my.crawler;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import com.lda.Corpus;
import com.lda.LdaGibbsSampler;
import com.lda.LdaUtil;

public class MyFuntions {

	public static void EachFileFCHandler(File f) {

		/*
		 * 为每个文本进行分词操作
		 */
		FileInputStream fis = null;
		BufferedReader br = null;
		FileOutputStream fout = null;
		PrintWriter pw = null;

		try {

			fis = new FileInputStream(f);
			br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
			fout = new FileOutputStream(MyConfig.FCFilesOutput + "\\"
					+ f.getName());
			pw = new PrintWriter(new OutputStreamWriter(fout, "UTF-8"));

		} catch (Exception e) {
			e.printStackTrace();
		}

		if (br != null) {
			String temp;
			try {
				while ((temp = br.readLine()) != null) {
					// System.out.println(temp + "\n");
					StringReader sr = new StringReader(temp);
					IKSegmenter ik = new IKSegmenter(sr, true);
					Lexeme lex = null;
					while ((lex = ik.next()) != null) {
						 System.out.print(lex.getLexemeText() + "  ");
						pw.print(lex.getLexemeText() + "\n");
					}
					sr = null;
					ik = null;
					lex = null;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		try {

			br.close();
			fis.close();
			pw.print("\r\n");
			pw.close();
			fout.close();

			br = null;
			fis = null;
			pw = null;
			fout = null;

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void runLda(String fileName) {

		/***** LDA的相关操作 ****/
		// 1. Load corpus from disk
		Corpus corpus = null;
		try {
			corpus = Corpus.load(MyConfig.LDAFileSource);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 这里是工程目录下，分词后的文档所在目录（即javaFC程序处理后的结果）
			// 2. Create a LDA sampler
		LdaGibbsSampler ldaGibbsSampler = new LdaGibbsSampler(
				corpus.getDocument(), corpus.getVocabularySize());
		// 3. Train it
		ldaGibbsSampler.gibbs(corpus.getDocument().length); // 设置主题个数
		// ldaGibbsSampler.gibbs(20);
		// 4. The phi matrix is a LDA model, you can use LdaUtil to explain it.
		double[][] phi = ldaGibbsSampler.getPhi();
		Map<String, Double>[] topicMap = LdaUtil.translate(phi,
				corpus.getVocabulary(), 10);
		LdaUtil.explain(topicMap, fileName); // 这个函数里可以设置结果输出位置
	}

	public static String WeiBoHandlerTime(String oldtime) {

		// 把爬取的微博时间格式化yyyy-mm-dd HH-MM-ss
		String newtime = null;
		String[] temps = oldtime.split(" ");
		int index = temps[0].indexOf("分");
		System.out.println(oldtime);
		if (index != -1) {
			newtime = getRealDate(temps[0].substring(0, index));
		} else {
			int jindex = temps[0].indexOf("今");
			if (jindex != -1) {
				// 表明是今天
				newtime = getTodayDate() + " " + temps[1].substring(0, 5);
			} else {
				// 表明不是今天
				int yueIndex = temps[0].indexOf("月");
				if (yueIndex != -1)
					newtime = getFormatDate(temps[0], temps[1].substring(0, 5));
				else
					try {
						newtime = temps[0] + " " + temps[1].substring(0, 8);
					} catch (Exception e) {
						e.printStackTrace();
						System.out.println("cont handler==>"+Arrays.toString(temps));
						newtime="1994-01-01 00:00:00";
					}

			}
		}
		return newtime;
	}

	public static String getTodayDate() {
		return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	}

	public static String getRealDate(String fenzhong) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
		return df.format(new Date(new Date().getTime()
				- Integer.parseInt(fenzhong) * 60 * 1000));
	}

	public static String getFormatDate(String yrtime, String sftime) {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		String temp = c.get(Calendar.YEAR) + "-";
		int yueIndex = yrtime.indexOf("月");
		int riIndex = yrtime.indexOf("日");
		String yue = yrtime.substring(0, yueIndex);
		String ri = yrtime.substring(yueIndex + 1, riIndex);
		temp += yue + "-" + ri + " " + sftime + ":00";

		return temp;
	}
	
	public static boolean clearFileDir(String sPath)
	{
		 //如果sPath不以文件分隔符结尾，自动添加文件分隔符  
	    if (!sPath.endsWith(File.separator)) {  
	        sPath = sPath + File.separator;  
	    }  
	    File dirFile = new File(sPath);  
	    //如果dir对应的文件不存在，或者不是一个目录，则退出  
	    if (!dirFile.exists() || !dirFile.isDirectory()) {  
				
			    System.out.println("dirfile.....succ--create==>"+dirFile.mkdir());
	    	return false;
	        
	    }  
	    boolean flag = true;  
	    //删除文件夹下的所有文件(包括子目录)  
	    File[] files = dirFile.listFiles();  
	    for (int i = 0; i < files.length; i++) {  
	        //删除子文件  
	        if (files[i].isFile()) {  
	            flag = deleteFile(files[i].getAbsolutePath());  
	            if (!flag) break;  
	        } //删除子目录  
	        else {  
	            flag = clearFileDir(files[i].getAbsolutePath());  
	            if (!flag) break;  
	        }  
	    }  
	    if (!flag) return false; 
	    return false;
	   
	}
	
	public static boolean deleteFile(String sPath) {  
	  boolean  flag = false;  
	    File file = new File(sPath);  
	    // 路径为文件且不为空则进行删除  
	    if (file.isFile() && file.exists()) {  
	        file.delete();  
	        flag = true;  
	    }  
	    return flag;  
	}
	
	
	
}
